/*
 * http://forum.tribalwars.net/showpost.php?p=3172119&postcount=10
 * http://en56.tribalwars.net/interface.php?func=get_config
 * http://en56.tribalwars.net/interface.php?func=get_building_info
 * http://en56.tribalwars.net/interface.php?func=get_unit_info
 * 
 * http://think2loud.com/reading-xml-with-jquery/
 * http://www.xml.com/pub/a/2007/10/10/jquery-and-xml.html
 */

function Config() {
	this.time = new Date().getTime();
	this.url = window.location.url;
	this.host = window.location.host;

	this.urls = new Object();
	this.urls.settings = "http://" + this.host + "/interface.php";
	this.urls.forum    = "http://" + this.host + "/forum.php";
	/*
	this.urls.forum    = "http://" + this.host + "/forum.php?game.php?village=&screen=info_command";
	this.urls.forum    = "http://" + this.host + "/forum.php?game.php?village=&screen=place";
	*/
	

	this.buildings = new Object();
	this.buildings.list = new Array();
	this.units = new Object();
	this.units.list = new Array();

	this.load = function() {
		var config = this;
		$.get(this.urls.settings, {func : "get_config"}, function(data){config.processSettings(data);});
	};

	this.processValue = function(obj, xml, name) {
		obj[name] = xml.find(name).text();
	};

	this.processSettings = function(data) {
		var xml = $(data);
		this.speed = xml.find("speed").text();
		this.unit_speed = xml.find("unit_speed").text();
		this.fake_limit = xml.find("fake_limit").text();
		
		var config = this;
		$.get(this.urls.settings, {func : "get_building_info"}, function(data){config.processBuildings(data);});
	};

	this.processUnits = function(data) {
		var xml = $(data);
		this.processUnit(xml, "spear", "Spear fighter");
		this.processUnit(xml, "sword", "Swordsman");
		this.processUnit(xml, "axe", "Axeman");
		this.processUnit(xml, "archer", "Archer");
		this.processUnit(xml, "spy", "Scout");
		this.processUnit(xml, "light", "Light cavalry");
		this.processUnit(xml, "marcher", "Mounted archer");
		this.processUnit(xml, "heavy", "Heavy cavalry");
		this.processUnit(xml, "ram", "Ram");
		this.processUnit(xml, "catapult", "Catapult");
		this.processUnit(xml, "knight", "Paladin");
		this.processUnit(xml, "snob", "Nobleman");
		this.processUnit(xml, "militia", "Militia");
		
		localStorage.setItem('twConfig', JSON.stringify(this));
		this.onloaded();
	};

	this.processUnit = function(xml, id, name) {
		var data = xml.find(id);
		if (!data)
			return;

		var unit = new Object;
		this.processValue(unit, data, "wood");
		this.processValue(unit, data, "stone");
		this.processValue(unit, data, "iron");
		this.processValue(unit, data, "pop");
		this.processValue(unit, data, "speed");
		this.processValue(unit, data, "attack");
		this.processValue(unit, data, "defense");
		this.processValue(unit, data, "defense_cavalry");
		this.processValue(unit, data, "defense_archer");
		this.processValue(unit, data, "carry");
		this.processValue(unit, data, "build_time");

		if(unit.speed.length > 0) {
			this.units.list.push(unit);
			this.units[id] = unit;
			unit.id = id;
			unit.name = name;
		}
	};

	this.processBuildings = function(data) {
		var xml = $(data);
		this.processBuilding(xml, "main",     "Village Headquarters", 10);
		this.processBuilding(xml, "barracks", "Barracks", 16);
		this.processBuilding(xml, "stable",   "Stable", 20);
		this.processBuilding(xml, "garage",   "Workshop", 24);
		this.processBuilding(xml, "church",   "Church", 10);
		this.processBuilding(xml, "church_f", "First Church", 10);
		this.processBuilding(xml, "snob",     "Academy", 512);
		this.processBuilding(xml, "smith",    "Smithy", 19);
		this.processBuilding(xml, "place",    "Rally point", 0);
		this.processBuilding(xml, "statue",   "Statue", 24);
		this.processBuilding(xml, "market",   "Market", 10);
		this.processBuilding(xml, "wood",     "Timber camp", 6);
		this.processBuilding(xml, "stone",    "Clay pit", 6);
		this.processBuilding(xml, "iron",     "Iron mine", 6);
		this.processBuilding(xml, "farm",     "Farm", 5);
		this.processBuilding(xml, "storage",  "Warehouse", 6);
		this.processBuilding(xml, "hide",     "Hiding place", 5);
		this.processBuilding(xml, "wall",     "Wall", 8);

		var config = this;
		$.get(this.urls.settings, {func : "get_unit_info"}, function(data){config.processUnits(data);});
	};

	this.processBuilding = function(xml, id, name, basePoints) {
		var data = xml.find(id);
		if (!data)
			return;

		var building = new Object;
		this.processValue(building, data, "max_level");
		this.processValue(building, data, "min_level");
		this.processValue(building, data, "wood");
		this.processValue(building, data, "stone");
		this.processValue(building, data, "iron");
		this.processValue(building, data, "pop");
		this.processValue(building, data, "wood_factor");
		this.processValue(building, data, "stone_factor");
		this.processValue(building, data, "iron_factor");
		this.processValue(building, data, "pop_factor");
		this.processValue(building, data, "build_time");
		this.processValue(building, data, "build_time_factor");
		
		if(building.pop.length > 0) {
			this.buildings.list.push(building);
			this.buildings[id] = building;
			building.id = id;
			building.name = name;
			building.basePoints = basePoints;
		}
	};
}


/*
 * http://en56.tribalwars.net/interface.php?func=get_config
 * 
 * config.
 * 
 * speed unit_speed moral build_queue
 * 
 * build.destroy build.buildtime_reduce build.buildtime_reduce_points
 * 
 * misc.log_losses misc.kill_ranking misc.tutorial misc.millis_arrival
 * misc.command_cancel_time misc.trade_cancel_time misc.free_premium_points
 * misc.free_premium_points2 misc.online_time misc.more_premium
 * misc.start_christmas_tree misc.end_christmas_tree
 * 
 * newbie.days newbie.ratio_days newbie.ratio
 * 
 * game.base_config game.knight game.knight_new_items game.archer game.tech
 * game.spy game.farm_limit game.church game.fake_limit game.barbarian_rise
 * game.barbarian_max_points game.hauls
 * 
 * buildings.base_setting buildings.hb_offset buildings.hb_factor
 * buildings.custom_main buildings.custom_farm buildings.custom_storage
 * buildings.custom_place buildings.custom_barracks buildings.custom_church
 * buildings.custom_church_f buildings.custom_smith buildings.custom_wood
 * buildings.custom_stone buildings.custom_iron buildings.custom_market
 * buildings.custom_stable buildings.custom_wall buildings.custom_garage
 * buildings.custom_hide buildings.custom_snob buildings.custom_statue
 * 
 * snob.gold snob.cheap_rebuild snob.simple snob.rise snob.max_dist snob.factor
 * snob.coin_wood snob.coin_stone snob.coin_iron
 * 
 * ally.wars_enabled ally.no_harm ally.no_other_support ally.no_leave
 * ally.no_admin ally.limit ally.fixed_allies ally.diplomacy
 * ally.reservation_manager ally.points_member_count ally.bypass_igm_block
 * ally.admin_bypass_igm_block ally.wars_member_requirement
 * ally.wars_points_requirement
 * 
 * win.check win.points win.villages win.hours
 * 
 * coord.map_size coord.func coord.empty_villages coord.bonus_villages
 * coord.bonus_new coord.inner coord.select_start coord.village_move_wait
 * coord.noble_restart coord.worldmap coord.legacy_scenery
 * 
 * sitter.allow
 * 
 * sleep.active sleep.delay sleep.min sleep.max sleep.min_awake sleep.max_awake
 * sleep.warn_time
 * 
 * night.active night.start_hour night.end_hour night.def_factor
 */
